import pytest
from common import log
from common import shell
from config import config
from common import mail
from common import consts
from common.tools import zip_dir

if __name__ == '__main__':
    conf = config.Config()
    log = log.Log.get_logger()
    log.info('初始化配置文件,path='+conf.log_path)

    shell = shell.Shell()
    xml_report_path = consts.XML_REPORT_PATH
    html_report_path = consts.HTML_REPORT_PATH
    cases_path = consts.CASE_PATH
    attach_file = 'report.zip'

    #定义测试集
    args = ['-s','-q','--alluredir',xml_report_path,cases_path]
    pytest.main(args)

    cmd = "allure generate {} -o {}".format(xml_report_path,html_report_path)

    try:
        shell.execute(cmd)
    except  Exception:
        log.error('执行用例失败，请检查环境配置')
        raise
    #压缩report文件夹
    zip_dir(consts.REPORT_PATH,attach_file)

    try:
        mail = mail.send_email(attach_file)
    except Exception as e:
        log .error('发送邮件失败，请检查邮件配置')
        raise